#include "countdown.h"

CountDown::CountDown(QObject *parent) : QObject(parent)
{
    timer = new QTimer(parent);
    lcd = new QLCDNumber(static_cast<QWidget *>(parent));
}
//获取宽度
int CountDown::getLCDWidth(){
    return lcd->width();
}
//设置显示的位置
void CountDown::setPos(QPoint pos){
    lcd->move(pos);
}

//设置倒计时
void CountDown::setTime(int ms){
    time = ms;
}

//开始计时
void CountDown::start_time(){
    lcd->display(time);
    lcd->show();
    connect(timer,&QTimer::timeout,this,&CountDown::change_lcd_num);
    timer->start();
}

//关闭倒计时和显示灯
void CountDown::close_lcd_timer(){
    if(timer->isActive())
        timer->stop();
    disconnect(timer,&QTimer::timeout,this,&CountDown::change_lcd_num);
    lcd->hide();
}

//计时1s
void CountDown::change_lcd_num(){
    lcd->display(static_cast<int>(lcd->value())-1);
    if(static_cast<int>(lcd->value()) < 0){
        qDebug() << "倒计时结束" ;
        //关闭倒计时，并解除信号连接
        close_lcd_timer();
        emit time_finish();
    }else{
        timer->start(1000);
    }
}

CountDown::~CountDown(){
    delete timer;
    delete lcd;
}
